package com.leijingqi.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.leijingqi.mapper.DeptMapper;
import com.leijingqi.mapper.EmpMapper;
import com.leijingqi.pojo.Emp;
import com.leijingqi.pojo.PageBean;
import com.leijingqi.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
//    @Override
//    public PageBean page(Integer page, Integer pageSize) {
//        //1 获取总记录数
//      Long count =  empMapper.count();
//      //2获取分页查询结果
//        Integer start = (page-1)*pageSize;
//      List<Emp> empList =  empMapper.page(start,pageSize);
//        //3 封装PageBean对象
//        PageBean pageBean = new PageBean(count,empList);
//        return  pageBean;
//    }

    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {

        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Emp> emplist =  empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>)  emplist;
        PageBean pageBean = new PageBean(p.getTotal(),p.getResult());
        return  pageBean;
    }


//    @Override
//    public PageBean page(Integer page, Integer pageSize, String name, short gender, LocalDate begin, LocalDate end) {
//        return null;
//    }

    @Override
    public void delete(List<Integer> ids) {
        empMapper.delete(ids);
    }

    @Override
    public void save(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
    }

    @Override
    public Emp searchID(Integer id) {
        Emp emp = empMapper.selecttDetails(id);
        return emp;
    }

    @Override
    public void updateUser(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateDetails(emp);
    }

    @Override
    public Emp login(Emp emp) {
        Emp e = empMapper.getByusernameAndPaddword(emp);
        return e;
    }


}
